//
//  AppDelegate.m
//  SmartLife
//
//  Created by Lawrence on 15/9/23.
//  Copyright (c) 2015年 Lawrence. All rights reserved.
//

#import "AppDelegate.h"
#import "SmartLifeListViewController.h"
#import "MenuViewController.h"
#import "IntroduceViewController.h"
#import <ZBarSDK.h>
#import "SmartLifeViewController.h"
#import "APIKey.h"


@interface AppDelegate (){
    CLLocationManager *_locationManager;
}
@property(strong,nonatomic) SmartLifeViewController *listView;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
    
    //判断程序是否为第一次启动
    if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]){
        
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        
        //如果是第一次启动的话，使用欢迎界面作为根视图
        IntroduceViewController *viewController=[[IntroduceViewController alloc] init];
        [self.window setRootViewController:viewController];
        
    }else{
        
        
        self.listView=[[SmartLifeViewController alloc] initStoryboardWithframe:self.window.frame];
        //添加导航栏
        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:_listView];
        
        [UINavigationBar appearance].barTintColor=[UIColor colorWithRed:253/255.0 green:121/255.0 blue:73/255.0 alpha:1];
        
        
        //导入侧滑框架
        MenuViewController *menuView=[[MenuViewController alloc] init];
        RESideMenu *sideMenu=[[RESideMenu alloc] initWithContentViewController:navController leftMenuViewController:menuView rightMenuViewController:nil];
        sideMenu.menuPreferredStatusBarStyle=1;
        sideMenu.delegate=self;
        sideMenu.contentViewShadowColor=[UIColor clearColor];
        sideMenu.backgroundImage=[UIImage imageNamed:@"background_blur"];
        sideMenu.contentViewShadowOffset=CGSizeMake(0, 0);
        sideMenu.contentViewShadowOpacity=0.6;
        sideMenu.contentViewShadowRadius=12;
        sideMenu.contentViewShadowEnabled=YES;
        
        
        //实现侧滑功能
        self.window.rootViewController=sideMenu;
    }
    
    [self.window makeKeyAndVisible];
    self.window.backgroundColor=[UIColor whiteColor];
    
    [ZBarReaderView class];
    
    [self initLocationManager];
    
    [self initAMapNaviConfig];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

-(void)initLocationManager{
    _locationManager=[[CLLocationManager alloc] init];
    
    if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined) {
        
        if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
            
            [_locationManager performSelector:@selector(requestAlwaysAuthorization)];
            
        }
        if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
            
            [_locationManager performSelector:@selector(requestWhenInUseAuthorization)];
            
        }
    }
}


-(void)initAMapNaviConfig{
    if ([APIKey length]==0) {
        NSLog(@"APIKey 不能为空");
    }
    
    [AMapNaviServices sharedServices].apiKey=(NSString *)APIKey;
    [MAMapServices sharedServices].apiKey=(NSString *)APIKey;
}



#pragma mark 禁止横屏
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:( UIWindow *)window{
    return UIInterfaceOrientationMaskPortrait;
}

@end

